import Vue from 'vue'
import VueRouter from 'vue-router'
import store from '../vuex'

//导入自定义vue
import Blog from '../views/blog/Blog.vue'
import Category from '../views/blog/Category.vue'
import AddBlog from '../views/blog/AddBlog.vue'
import Homework from '../views/homework/Homework.vue'
import Login from '../views/Login.vue'
import Home from '../views/Home.vue'
import Welcome from '../views/Welcome.vue'
import User from '../views/sys/User.vue'
import Role from '../views/sys/Role.vue'
import Dict from '../views/sys/Dict.vue'

//使用
Vue.use(VueRouter)

//定义
const routes = [{
  path: '/',
  redirect: '/login' // 重定向
},
{
  path: '/login', // 登录页
  component: Login
},

// 典型的后台框架 路由
{
  path: '/home',
  component: Home,
  redirect: '/welcome', // 重定向，默认显示welcome页
  // 子路由===》子菜单注册到Home的子路由
  children: [
    // 后台功能菜单
    {
      path: '/welcome',
      component: Welcome
    },
    {
      path: '/blog',
      component: Blog
    },
     
    {
      path: '/addblog',
      component: AddBlog
    },
    {
      path: '/category',
      component: Category
    },
    {
      path: '/homework',
      component: Homework
    },
    
    {
      path: '/user',
      component: User
    },
    {
      path: '/role',
      component: Role
    },
    {
      path: '/dict',
      component: Dict
    },
  ]

}
]

//new 路由
const router = new VueRouter({
  //可以改路由模式 
  routes
})


// 前端路由拦截 
// 添加路由守卫
// 如果用户没有登录，不能访问/home,如果用户通过url地址直接访问，则强制跳转到登录页面
// 挂载路由导航守卫,to表示将要访问的路径，from表示从哪里来，next是下一个要做的操作
router.beforeEach((to, from, next) => {
  // console.log(to,from)
  if (to.path === '/login') { return next() }
  // 获取token
  const tokenStr = store.state.token
  console.log("---router",tokenStr)

  if (!tokenStr) { return next('/login') }

  next()
})


// 抛出去    export default
export default router
